<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Redis;

class Admin extends Model
{
    use HasFactory;
    protected $table="admin";
    protected $primaryKey='id';

    const LOGIN_PREFIX = 'login:user:';

    /**
     * 根据token获取用户信息
     */
    public static function getInfoByToken(string $token = null): array
    {
        $md5Token = self::LOGIN_PREFIX . md5($token);
        if ( !Redis::get($md5Token)) {
            return [];
        }
        Redis::set($md5Token, Redis::get($md5Token), 10800);
        $info = Redis::get($md5Token);
        return json_decode($info,true);
    }
}
